package EGS.impl;
import java.util.concurrent.ExecutorService;

import javax.swing.JTable;

import org.dom4j.Document;
import org.dom4j.Element;
import TOOLS.SQLtool;
import TOOLS.errCodeManagement;
import WORKPLACE.m_SNinfo;
import WORKPLACE.stationClient;

public class EGS_execute_service10025Impl{
	public boolean isUnknowCase = false;
	ExecutorService iExecutorService;
	Document MSG;
	boolean isPanel = false;
	stationClient iClient=null;
	SQLtool sql = null;
	/**
	 * 10025 获取SN信息
	 */
	public EGS_execute_service10025Impl(stationClient nClient, Document MSG_in, ExecutorService executorService, 
			SQLtool goldenSql) throws Exception {
		super();
		sql=goldenSql;
		if(nClient!=null) {
			iClient=nClient;
		}else {
			iClient=null;
		}
		MSG = MSG_in;
		iExecutorService = executorService;
	}

	public Document runCal() {
		String msgClass = "";
		try {
			if (MSG == null) {
				return null;
			}
			Element Mroot = MSG.getRootElement();
			msgClass = Mroot.element("header").attribute("messageClass").getValue();
			String iStation, iSN;

			iStation = Mroot.element("header").element("location").attribute("equipmentName").getValue();

			if (msgClass.equals("10025")) {// 获取SN信息
				iSN = Mroot.element("body").element("pcb").attribute("barcode").getValue();
				Mroot.element("body").remove(Mroot.element("body").element("pcb"));
				Element eleR = Mroot.element("body").addElement("result");
				Element eleW = Mroot.element("body").addElement("wipno");

				try {
					m_SNinfo result = iClient.getSNinfo(iSN);// 如果工序是A0200强制重回FCT，除非没有FCT工序
					if (result == null) {
						isUnknowCase = true;
						return null;
//						throw new Exception("条码信息不存在");	// 查不到的去32
					}
					JTable t=sql.select("select createTime from order_list where orderNumber='"+result.lotNo+"'");
					if(t!=null && t.getRowCount()>0) {
						result.createTime=""+t.getValueAt(0, 0);
					}
					eleW.addAttribute("referencewipno", result.panelWipno);
					eleW.addAttribute("subwipno", iSN);
					eleW.addAttribute("position", "" + result.position);
					eleW.addAttribute("referencelotno", result.panelLotNo);
					eleW.addAttribute("sublotno", result.lotNo);
					eleW.addAttribute("sublotdate",result.createTime);
					eleW.addAttribute("product", result.number);
					eleW.addAttribute("bin", result.bin);
					eleW.addAttribute("bin_laser", result.led1);
					eleW.addAttribute("bin_label", result.led2);
					eleW.addAttribute("bin_package", result.led3);
					eleW.addAttribute("bin_FCT", result.led4);
					eleW.addAttribute("productName", result.name);
					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					AKSLogs(100251, "" + iSN + "_" + iStation);
				} catch (Exception e) {
					isUnknowCase = true;
					return null;
				}
			} else {
				isUnknowCase = true;
				return null;
			}
			return MSG;
		} catch (Exception e) {
			if(iClient==null) {
				Element Mroot = MSG.getRootElement();
				Element eleR = Mroot.element("body").addElement("result");
				eleR.addAttribute("errorCode", "1");
				eleR.addAttribute("errorText", "工位信息获取失败");
				eleR.addAttribute("actionCode", "0");
				return MSG;
			}
			String str = errCodeManagement.getErrText(e.getMessage());
			System.out.println(str);
			return null;
		}

	}

	public void AKSLogs(int iType, String ilogClientNews) {
		SQLtool.AKSLogs(iType, ilogClientNews);
	}

}
